﻿using lumo;
using lumo.display.animation2D;
using lumo.display.object2D;
using lumo.scene;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Media;

namespace game.scene
{

    /// <summary>
    /// Scene where it will be said
    /// </summary>
    public class SceneCredits : IScene
    {

        protected const string _FontName = "Fonts/Arial";
        protected string[] _StringList = { "LUMO", "Engine", "LUMO Engine", "Programmer", "Bruno Miguel de Oliveira Tamer", "Artist", "Tiago Felix", "Game Designer", "Thiago Sardenberg" };
        protected Label[] _LabelList;
        protected SerialAnimation2D _LabelAnimation = new SerialAnimation2D();
        protected const int _SPACEMENT = 20;
        protected const float _TIME = 60000f;
        protected const float _PRE = 1000.0f;
        protected const float _MID = 1000.0f;
        protected const float _POS = 1000.0f;
        protected IScene _PreviousScene;

        public SceneCredits(IScene from)
        {
            _PreviousScene = from;
        }

        /// <summary>
        /// Start the scene
        /// </summary>
        /// <param name="Component"></param>
        public void Start(LumoComponent Component)
        {
            if (_PreviousScene is SceneMap)
            {
                Song song = Component.Content.Load<Song>("Audio/BGM/Title");
                MediaPlayer.Play(song);
                MediaPlayer.IsRepeating = false;
            }

            if (_StringList.Length == 0)
                Component.Scene = new SceneTitle(this);

            SpriteFont spriteFont = Component.Content.Load<SpriteFont>(_FontName);
            _LabelList = new Label[_StringList.Length];

            ParallelAnimation2D miniParallel;
            SerialAnimation2D miniSerial;

            for (int i = 0; i < _LabelList.Length; i++)
            {
                _LabelList[i] = new Label(_StringList[i], spriteFont);
                _LabelList[i].Scale = Vector2.Zero;
                _LabelList[i].Color = Color.Transparent;
                _LabelList[i].Origin = new Vector2(_LabelList[i].Size.X / 2, _LabelList[i].Size.Y / 2);

                _LabelList[i].X = _LabelList[i].Origin.X + (Global.DefaultScreenSize.X - _LabelList[i].Size.X) / 2;
                _LabelList[i].Y = _LabelList[i].Origin.Y + Global.DefaultScreenSize.Y;

                miniSerial = new SerialAnimation2D(_LabelList[i]);

                miniParallel = new ParallelAnimation2D(_LabelList[i]);
                    miniParallel.Add(new ColorAnimation2D(Color.White, _PRE, Smooth.Arrival));
                    miniParallel.Add(new ScaleAnimation2D(Vector2.One, _PRE, Smooth.Arrival));
                    miniParallel.Add(new PositionAnimation2D(new Vector2(_LabelList[i].X, _LabelList[i].Origin.Y + (Global.DefaultScreenSize.Y - _LabelList[i].Size.Y) / 2), _PRE, Smooth.Arrival));
                miniSerial.Add(miniParallel);

                miniSerial.Add(new WaitAnimation2D(_MID));

                miniParallel = new ParallelAnimation2D(_LabelList[i]);
                    miniParallel.Add(new ColorAnimation2D(Color.Transparent, _POS, Smooth.Start));
                    miniParallel.Add(new ScaleAnimation2D(Vector2.Zero, _POS, Smooth.Start));
                    miniParallel.Add(new PositionAnimation2D(new Vector2(_LabelList[i].X, _LabelList[i].Origin.Y - _LabelList[i].Size.Y), _POS, Smooth.Start));
                miniSerial.Add(miniParallel);

                _LabelAnimation.Add(miniSerial);
            }

            _LabelAnimation.Add(new LambdaAnimation2D(Action => Component.Scene = new SceneTitle(this)));
        }

        /// <summary>
        /// Update the scene.
        /// </summary>
        /// <param name="Component"></param>
        public void Update(LumoComponent Component)
        {
            _LabelAnimation.Update(Component);
        }

        /// <summary>
        /// Draw the scene.
        /// </summary>
        /// <param name="Component"></param>
        public void Draw(LumoComponent Component)
        {
            Component.GraphicsDevice.Clear(Color.Black);
            Component.SpriteBatch.Begin(SpriteSortMode.Immediate, null, null, null, null, null, Global.ScreenMatrix);
            for (int i = 0; i < _LabelList.Length; i++)
                _LabelList[i].Draw(Component.SpriteBatch);
            Component.SpriteBatch.End();
        }

        /// <summary>
        /// Terminate the scene
        /// </summary>
        /// <param name="Game"></param>
        public void Terminate(LumoComponent Component)
        {
        }

    }

}
